package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Banner;
import com.example.service.BannerService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "Banner管理") //swagger的注释
@CrossOrigin
@RequestMapping("/banner")
@RestController
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @ApiOperation(value = "获取Banner")
    @GetMapping("getBanner")
    public R getBanner() {
        QueryWrapper<Banner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        List<Banner> list = bannerService.list(wrapper);
        return R.ok().data("list", list);
    }


    @ApiOperation(value = "添加Banner")
    @PostMapping("addBanner")
    public R addBanner(
            @ApiParam(name = "banner", value = "轮播图", required = true) @RequestBody Banner banner) {
        boolean b = bannerService.save(banner);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("deleteBanner/{bannerid}")
    public R deleteBanner(@PathVariable String bannerid) {
        boolean b = bannerService.removeById(bannerid);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}
